<?php

namespace App\Http\Model\Origami;

use App\Http\Model\Base;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class UserOrigami extends Base
{
    //折纸status
    const STATUS_OF_PUBLISHED = 1;//已发布
    const STATUS_OF_ROLLBACK = 0;//已撤回

    //审核audit_status
    const AUDIT_STATUS_OF_AUDIT_FAIL = 0;//审核失败
    const AUDIT_STATUS_OF_AUDIT_SUCCESS = 1;//审核通过
    const AUDIT_STATUS_OF_TO_BE_RECEIVED = 2;//待审查

    /**
     * 折纸缓存
     *
     * @var string
     */
    private $cacheKeyPrefix = 'ORIGAMI:USER:';

    /**
     * 表名字
     *
     * @var string
     */
    public $table = 'user_origami';

    /**
     * @var bool
     */
    public $timestamps = true;

    /**
     * 模型日期列的存储格式。
     *
     * @var string
     */
    protected $dateFormat = 'U';

    /**
     * 可以被批量赋值的属性。
     *
     * @var array
     */
    protected $fillable = ['id', 'user_id', 'type_id', 'content', 'status', 'audit_status', 'created_at', 'updated_at'];

    /**
     * 构造函数
     *
     * @param array $attributes
     */
    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 获取缓存前缀
     *
     * @param int $userId
     * @return string
     */
    private function getCacheKey(int $userId): string
    {
        return $this->cacheKeyPrefix . $userId;
    }

    /**
     * 获取用户折纸内容
     *
     * @param int $userId
     * @return array
     */
    public function getUserOrigami(int $userId): array
    {
        $cacheKey = $this->getCacheKey($userId);
        $cache = Redis::GET($cacheKey);
        if ($cache) {
            return json_decode($cache, true);
        }

        $this->clearAndResetCache($userId);
        return json_decode(Redis::GET($cacheKey), true);
    }

    /**
     * 重置缓存
     *
     * @param int $userId
     * @return bool
     */
    public function clearAndResetCache(int $userId): bool
    {
        $cacheKey = $this->getCacheKey($userId);
        Redis::DEL($cacheKey);
        $record = self::query()->where('user_id', $userId)->limit(1)->get()->toArray();
        Redis::SET($cacheKey, json_encode($record ? $record[0] : []));
        Redis::EXPIRE($cacheKey, 30 * 24 * 3600);//过期时间一个月
        return true;
    }

    /**
     * 修改审核状态
     *
     * @param int $id
     * @param int $auditStatus 0/1
     * @return int
     */
    public function checkUserOrigami($id, $auditStatus)
    {
        $records = ['audit_status' => $auditStatus];
        if ($auditStatus == self::AUDIT_STATUS_OF_AUDIT_FAIL) {//审核失败
            $records['status'] = self::STATUS_OF_ROLLBACK;//重置为 已撤回
        }

        return DB::table($this->table)->where('id', $id)->update($records);
    }

    /**
     * 获取单条信息
     * @param $id
     * @return \Illuminate\Database\Query\Builder|mixed
     */
    public function getFirstByid($id)
    {
        $res = DB::table($this->table)->find($id);
        return $this->objToArray($res);
    }
}
